//
//  MCAlertView.m
//
//
//  Created by menhao on 17/6/12.
//
//

#import "MCAlertView.h"
#import "MCMSPopView.h"
#import "MCMoneyPopView.h"

static MCAlertView *_alertWindow;

@interface MCAlertView ()


#pragma  mark - property

@property (nonatomic,weak)  UIButton *btBtn;

@property (nonatomic,strong)UIButton * doneInKeyboardButton;

@end

@implementation MCAlertView

#pragma mark - Init

+ (instancetype)alertInstance{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _alertWindow = [[self alloc] init];
        [_alertWindow setUpUI];
    });
    return _alertWindow;
}


- (void)setUpUI{
    
    self.frame = CGRectMake(0, G_SCREENHEIGHT - 49, G_SCREENWIDTH, 0);
    self.backgroundColor = MC_ColorWithAlpha(228, 228, 228, 1);
    
    UILabel *titleSelectedInfoLabel = [[UILabel alloc] init];
    [self addSubview:titleSelectedInfoLabel];
    titleSelectedInfoLabel.text = @"已选0，0元";
    titleSelectedInfoLabel.font = [UIFont systemFontOfSize:13.0f];
    titleSelectedInfoLabel.textColor = [UIColor darkGrayColor];
    self.titleSelectedInfoLabel = titleSelectedInfoLabel;
    
    
    UILabel *titleWinInfoLabel = [[UILabel alloc] init];
    [self addSubview:titleWinInfoLabel];
    titleWinInfoLabel.text = @"若中奖，人格个人已选0，0元";
    self.titleWinInfoLabel = titleWinInfoLabel;
    titleWinInfoLabel.font = [UIFont systemFontOfSize:13.0f];
    titleWinInfoLabel.textColor = [UIColor darkGrayColor];
    
    
    UIButton *btBtn = [[UIButton alloc] init];
    [btBtn setTitle:@"倍投" forState:UIControlStateNormal];
    [btBtn.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
    self.btBtn = btBtn;
    btBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    btBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -50, 0, 0);
    UITextField *btTF = [[UITextField alloc] init];
    [btBtn addSubview:btTF];
    [btBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self addSubview:btBtn];
    self.btTF = btTF;
    btTF.textAlignment = NSTextAlignmentCenter;
    btTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"1" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15.0f],NSForegroundColorAttributeName : [UIColor blackColor]}];
    btTF.keyboardType = UIKeyboardTypeNumberPad;
    btTF.clearButtonMode = UITextFieldViewModeNever;
    btTF.returnKeyType = UIReturnKeyDone;
    btTF.inputAccessoryView = [self addToolbar];
    
    [btTF addTarget:self action:@selector(action_EditingChanged:) forControlEvents:(UIControlEventEditingChanged)];
    
    
    UIButton *msBtn = [[UIButton alloc] init];
    [msBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [msBtn.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
    [self addSubview:msBtn];
    [msBtn setTitle:@"元模式" forState:UIControlStateNormal];
    self.msBtn = msBtn;
    
    
    UIButton *moneyBtn = [[UIButton alloc] init];
    [self addSubview:moneyBtn];
    self.moneyBtn = moneyBtn;
    [moneyBtn setTitle:@"1982.0.0" forState:UIControlStateNormal];
    [moneyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [moneyBtn.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
    
    
    btBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    btBtn.layer.borderWidth = 0.5f;
    btBtn.layer.masksToBounds = YES;
    msBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    msBtn.layer.borderWidth = 0.5f;
    msBtn.layer.cornerRadius = 4.0f;
    msBtn.layer.masksToBounds = YES;
    moneyBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    moneyBtn.layer.borderWidth = 0.5f;
    moneyBtn.layer.cornerRadius = 4.0f;
    moneyBtn.layer.masksToBounds = YES;
    [msBtn addTarget:self action:@selector(msBtnClick:) forControlEvents:UIControlEventTouchDown];
    [moneyBtn addTarget:self action:@selector(moneyBtnClick:) forControlEvents:UIControlEventTouchDown];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyBoard) name:@"HEY_BOARD_HIDEN" object:nil];
    
    
    
    
    
#pragma mark-监听键盘的回收
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}


- (UIToolbar *)addToolbar{
    
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, G_SCREENWIDTH, 35)];
    toolbar.tintColor = RGB(58, 130, 208);
    toolbar.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *Button10 = [[UIBarButtonItem alloc] initWithTitle:@"10倍" style:UIBarButtonItemStylePlain target:self action:@selector(TextField10)];
    UIBarButtonItem *Button100 = [[UIBarButtonItem alloc] initWithTitle:@"100倍" style:UIBarButtonItemStylePlain target:self action:@selector(TextField100)];
    UIBarButtonItem *Button1000 = [[UIBarButtonItem alloc] initWithTitle:@"1000倍" style:UIBarButtonItemStylePlain target:self action:@selector(TextField1000)];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(textFieldDone)];
    toolbar.items = @[Button10, Button100,Button1000, space, bar];
    return toolbar;
}
//10倍
-(void)TextField10{
    self.btTF.text=@"10";
}
//100倍
-(void)TextField100{
    self.btTF.text=@"100";
}
//1000倍
-(void)TextField1000{
    self.btTF.text=@"1000";
}
//完成
-(void)textFieldDone{
    [self.btTF resignFirstResponder];
    
}

#pragma mark - Layout
- (void)layoutSubviews{
    
    [super layoutSubviews];
    CGFloat btnHeight = 30;
    CGFloat padding = 7.0f;
    
    [self.titleSelectedInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(@(padding));
    }];
    
    [self.titleWinInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.titleSelectedInfoLabel.mas_bottom).offset(padding);
    }];
    
    [self.btBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleWinInfoLabel.mas_bottom).offset(padding);
        make.left.equalTo(self.mas_left).offset(2*padding);
        make.height.equalTo(@(btnHeight));
        make.width.equalTo(@((self.widht - padding * 4) * 0.3));
    }];
    
    [self.msBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleWinInfoLabel.mas_bottom).offset(padding);
        make.left.equalTo(self.btBtn.mas_right).offset(2*padding);
        make.height.equalTo(@(btnHeight));
        make.width.equalTo(@((self.widht - padding * 4) * 0.3));
    }];
    
    [self.moneyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleWinInfoLabel.mas_bottom).offset(padding);
        make.left.equalTo(self.msBtn.mas_right).offset(2*padding);
        make.right.equalTo(self.mas_right).offset(-2 * padding);
        make.height.equalTo(@(btnHeight));
        
    }];
    
    [self.btTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.btBtn.mas_right);
        make.centerY.equalTo(self.btBtn.mas_centerY);
        make.top.equalTo(self.btBtn.mas_top);
        make.bottom.equalTo(self.btBtn.mas_bottom);
        make.width.equalTo(@(50));
    }];
    
}

#pragma mark - show and hide window

- (void)showStackWindow{
    

    self.hidden = NO;

    self.frame = CGRectMake(0, G_SCREENHEIGHT- MC_REALVALUE(146), G_SCREENWIDTH, MC_REALVALUE(146));

    
    self.hidden = NO;
}
- (void)showStackWindowWithHeight:(CGFloat)height{
    
    self.hidden = NO;

    self.frame = CGRectMake(0, G_SCREENHEIGHT- height, G_SCREENWIDTH, height);

    self.hidden = NO;
}

- (void)hideStackWindow{
    if (self.hidden) {
        return;
    }

    [self popViewHidden];
    
    self.frame = CGRectMake(0, G_SCREENHEIGHT - 49, G_SCREENWIDTH, 0);

    self.hidden = YES;

}

#pragma mark - touch event
/**
 *
 *金额点击弹出框
 **/
- (void)moneyBtnClick:(UIButton *)btn{
    if ([self.btTF resignFirstResponder]) {
        [self endEditing:YES];
    } else {
        
        MCMoneyPopView *popView = [MCMoneyPopView alertInstance];
        popView.frame = CGRectMake(self.moneyBtn.left , G_SCREENHEIGHT- 45 - 49-120, self.moneyBtn.widht, 120);
        
        __weak MCAlertView *weakSelf = self;
        popView.MoneySelectRowBlock = ^(NSString *type){
            [weakSelf.moneyBtn setTitle:type forState:UIControlStateNormal];
        };
        [popView showModelWindow];
    }
}

/**
 *
 *元角模式点击弹出框
 **/
- (void)msBtnClick:(UIButton *)btn{
    if ([self.btTF resignFirstResponder]) {
        [self endEditing:YES];
    } else {
        
        MCMSPopView *popView = [MCMSPopView alertInstance];
        popView.frame = CGRectMake(self.msBtn.left, G_SCREENHEIGHT- 45 - 49 - 92 , self.msBtn.widht, 92);
        
        __weak MCAlertView *weakSelf = self;
        popView.MSSelectRowBlock = ^(NSString *type){
            [weakSelf.msBtn setTitle:type forState:UIControlStateNormal];
            //修改元角分模式-》发送通知
            [[NSNotificationCenter defaultCenter] postNotificationName:@"Action_YuanJiaoFen" object:nil userInfo:@{@"type":type}];
        };
        [popView showModelWindow];
    }
}

#pragma mark-修改完倍投后   发送通知
- (void) keyboardWillHide : (NSNotification*)notification {
    
    
    if ([_btTF.text intValue]==0) {
        _btTF.text=@"1";
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Action_Multiple" object:nil userInfo:@{@"multiple":_btTF.text}];
    
}

-(void)action_btn_finish{
    [self.btTF resignFirstResponder];
}
#pragma mark -- 输入变化
- (void)action_EditingChanged:(UITextField *)textField
{
    
    if (textField.text.length>=4) {
        textField.text=[textField.text substringToIndex:4];
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self endEditing:YES];
}

- (void)hideKeyBoard{
    if ([self.btTF resignFirstResponder]) {
        [self endEditing:YES];
    }
    
}
-(void)popViewHidden{
    MCMoneyPopView *popView = [MCMoneyPopView alertInstance];
    [popView hideModelWindow];
    MCMSPopView *popView2 = [MCMSPopView alertInstance];
    [popView2 hideModelWindow];
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
